Example: Train sizing¶
This example shows how to asses a model's performance based on the size of the training set.
The data used is a variation on the Australian weather dataset from Kaggle. You can download it from here. The goal of this dataset is to predict whether or not it will rain tomorrow training a binary classifier on target RainTomorrow.
Load the data¶
In [1]:
Copied!
# Import packages
import pandas as pd
from atom import ATOMClassifier
# Import packages
import pandas as pd
from atom import ATOMClassifier
In [2]:
Copied!
# Load the data
X = pd.read_csv("docs_source/examples/datasets/weatherAUS.csv")
# Let's have a look
X.head()
# Load the data
X = pd.read_csv("docs_source/examples/datasets/weatherAUS.csv")
# Let's have a look
X.head()
Out[2]:
| Location | MinTemp | MaxTemp | Rainfall | Evaporation | Sunshine | WindGustDir | WindGustSpeed | WindDir9am | WindDir3pm | ... | Humidity9am | Humidity3pm | Pressure9am | Pressure3pm | Cloud9am | Cloud3pm | Temp9am | Temp3pm | RainToday | RainTomorrow | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | MelbourneAirport | 18.0 | 26.9 | 21.4 | 7.0 | 8.9 | SSE | 41.0 | W | SSE | ... | 95.0 | 54.0 | 1019.5 | 1017.0 | 8.0 | 5.0 | 18.5 | 26.0 | Yes | 0 |
| 1 | Adelaide | 17.2 | 23.4 | 0.0 | NaN | NaN | S | 41.0 | S | WSW | ... | 59.0 | 36.0 | 1015.7 | 1015.7 | NaN | NaN | 17.7 | 21.9 | No | 0 |
| 2 | Cairns | 18.6 | 24.6 | 7.4 | 3.0 | 6.1 | SSE | 54.0 | SSE | SE | ... | 78.0 | 57.0 | 1018.7 | 1016.6 | 3.0 | 3.0 | 20.8 | 24.1 | Yes | 0 |
| 3 | Portland | 13.6 | 16.8 | 4.2 | 1.2 | 0.0 | ESE | 39.0 | ESE | ESE | ... | 76.0 | 74.0 | 1021.4 | 1020.5 | 7.0 | 8.0 | 15.6 | 16.0 | Yes | 1 |
| 4 | Walpole | 16.4 | 19.9 | 0.0 | NaN | NaN | SE | 44.0 | SE | SE | ... | 78.0 | 70.0 | 1019.4 | 1018.9 | NaN | NaN | 17.4 | 18.1 | No | 0 |
5 rows × 22 columns
Run the pipeline¶
In [3]:
Copied!
# Initialize atom and prepare the data
atom = ATOMClassifier(X, verbose=2, random_state=1)
atom.clean()
atom.impute(strat_num="median", strat_cat="most_frequent", max_nan_rows=0.8)
atom.encode()
# Initialize atom and prepare the data
atom = ATOMClassifier(X, verbose=2, random_state=1)
atom.clean()
atom.impute(strat_num="median", strat_cat="most_frequent", max_nan_rows=0.8)
atom.encode()
<< ================== ATOM ================== >> Configuration ==================== >> Algorithm task: Binary classification. Dataset stats ==================== >> Shape: (142193, 22) Train set size: 113755 Test set size: 28438 ------------------------------------- Memory: 25.03 MB Scaled: False Missing values: 316559 (10.1%) Categorical features: 5 (23.8%) Duplicates: 45 (0.0%) Fitting Cleaner... Cleaning the data... Fitting Imputer... Imputing missing values... --> Dropping 161 samples for containing more than 16 missing values. --> Imputing 481 missing values with median (12.0) in feature MinTemp. --> Imputing 265 missing values with median (22.6) in feature MaxTemp. --> Imputing 1354 missing values with median (0.0) in feature Rainfall. --> Imputing 60682 missing values with median (4.8) in feature Evaporation. --> Imputing 67659 missing values with median (8.4) in feature Sunshine. --> Imputing 9187 missing values with most_frequent (W) in feature WindGustDir. --> Imputing 9127 missing values with median (39.0) in feature WindGustSpeed. --> Imputing 9852 missing values with most_frequent (N) in feature WindDir9am. --> Imputing 3617 missing values with most_frequent (SE) in feature WindDir3pm. --> Imputing 1187 missing values with median (13.0) in feature WindSpeed9am. --> Imputing 2469 missing values with median (19.0) in feature WindSpeed3pm. --> Imputing 1613 missing values with median (70.0) in feature Humidity9am. --> Imputing 3449 missing values with median (52.0) in feature Humidity3pm. --> Imputing 13863 missing values with median (1017.6) in feature Pressure9am. --> Imputing 13830 missing values with median (1015.2) in feature Pressure3pm. --> Imputing 53496 missing values with median (5.0) in feature Cloud9am. --> Imputing 56933 missing values with median (5.0) in feature Cloud3pm. --> Imputing 743 missing values with median (16.7) in feature Temp9am. --> Imputing 2565 missing values with median (21.1) in feature Temp3pm. --> Imputing 1354 missing values with most_frequent (No) in feature RainToday. Fitting Encoder... Encoding categorical columns... --> Target-encoding feature Location. Contains 49 classes. --> Target-encoding feature WindGustDir. Contains 16 classes. --> Target-encoding feature WindDir9am. Contains 16 classes. --> Target-encoding feature WindDir3pm. Contains 16 classes. --> Ordinal-encoding feature RainToday. Contains 2 classes.
In [4]:
Copied!
# Analyze the impact of the training set's size on a LR model
atom.train_sizing("LR", train_sizes=10, n_bootstrap=5)
# Analyze the impact of the training set's size on a LR model
atom.train_sizing("LR", train_sizes=10, n_bootstrap=5)
Training ========================= >> Metric: f1 Run: 0 =========================== >> Models: LR01 Size of training set: 11362 (10%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5624 Test evaluation --> f1: 0.5857 Time elapsed: 0.721s Bootstrap --------------------------------------- Evaluation --> f1: 0.585 ± 0.0021 Time elapsed: 0.729s ------------------------------------------------- Time: 1.449s Final results ==================== >> Total time: 2.053s ------------------------------------- LogisticRegression --> f1: 0.585 ± 0.0021 Run: 1 =========================== >> Models: LR02 Size of training set: 22724 (20%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.582 Test evaluation --> f1: 0.5874 Time elapsed: 0.853s Bootstrap --------------------------------------- Evaluation --> f1: 0.5851 ± 0.002 Time elapsed: 0.865s ------------------------------------------------- Time: 1.718s Final results ==================== >> Total time: 2.425s ------------------------------------- LogisticRegression --> f1: 0.5851 ± 0.002 Run: 2 =========================== >> Models: LR03 Size of training set: 34087 (30%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5812 Test evaluation --> f1: 0.585 Time elapsed: 1.086s Bootstrap --------------------------------------- Evaluation --> f1: 0.5861 ± 0.0009 Time elapsed: 1.119s ------------------------------------------------- Time: 2.205s Final results ==================== >> Total time: 3.035s ------------------------------------- LogisticRegression --> f1: 0.5861 ± 0.0009 Run: 3 =========================== >> Models: LR04 Size of training set: 45449 (40%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5828 Test evaluation --> f1: 0.5862 Time elapsed: 1.173s Bootstrap --------------------------------------- Evaluation --> f1: 0.5863 ± 0.0017 Time elapsed: 1.282s ------------------------------------------------- Time: 2.455s Final results ==================== >> Total time: 3.365s ------------------------------------- LogisticRegression --> f1: 0.5863 ± 0.0017 Run: 4 =========================== >> Models: LR05 Size of training set: 56812 (50%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5823 Test evaluation --> f1: 0.5853 Time elapsed: 1.264s Bootstrap --------------------------------------- Evaluation --> f1: 0.585 ± 0.0016 Time elapsed: 1.460s ------------------------------------------------- Time: 2.724s Final results ==================== >> Total time: 3.758s ------------------------------------- LogisticRegression --> f1: 0.585 ± 0.0016 Run: 5 =========================== >> Models: LR06 Size of training set: 68174 (60%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5835 Test evaluation --> f1: 0.5843 Time elapsed: 1.392s Bootstrap --------------------------------------- Evaluation --> f1: 0.585 ± 0.0016 Time elapsed: 1.704s ------------------------------------------------- Time: 3.095s Final results ==================== >> Total time: 4.151s ------------------------------------- LogisticRegression --> f1: 0.585 ± 0.0016 Run: 6 =========================== >> Models: LR07 Size of training set: 79536 (70%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5872 Test evaluation --> f1: 0.5846 Time elapsed: 1.585s Bootstrap --------------------------------------- Evaluation --> f1: 0.5852 ± 0.0013 Time elapsed: 1.836s ------------------------------------------------- Time: 3.421s Final results ==================== >> Total time: 4.664s ------------------------------------- LogisticRegression --> f1: 0.5852 ± 0.0013 Run: 7 =========================== >> Models: LR08 Size of training set: 90899 (80%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5889 Test evaluation --> f1: 0.5841 Time elapsed: 1.693s Bootstrap --------------------------------------- Evaluation --> f1: 0.5852 ± 0.0025 Time elapsed: 2.139s ------------------------------------------------- Time: 3.832s Final results ==================== >> Total time: 5.157s ------------------------------------- LogisticRegression --> f1: 0.5852 ± 0.0025 Run: 8 =========================== >> Models: LR09 Size of training set: 102261 (90%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5871 Test evaluation --> f1: 0.5837 Time elapsed: 1.754s Bootstrap --------------------------------------- Evaluation --> f1: 0.5844 ± 0.0022 Time elapsed: 2.353s ------------------------------------------------- Time: 4.107s Final results ==================== >> Total time: 5.464s ------------------------------------- LogisticRegression --> f1: 0.5844 ± 0.0022 Run: 9 =========================== >> Models: LR10 Size of training set: 113624 (100%) Size of test set: 28408 Results for LogisticRegression: Fit --------------------------------------------- Train evaluation --> f1: 0.5856 Test evaluation --> f1: 0.585 Time elapsed: 1.978s Bootstrap --------------------------------------- Evaluation --> f1: 0.5846 ± 0.0005 Time elapsed: 2.544s ------------------------------------------------- Time: 4.521s Final results ==================== >> Total time: 5.975s ------------------------------------- LogisticRegression --> f1: 0.5846 ± 0.0005
Analyze the results¶
In [5]:
Copied!
# The results are now multi-index, where frac is the fraction
# of the training set used to fit the model. The model names
# end with the fraction as well (without the dot)
atom.results
# The results are now multi-index, where frac is the fraction
# of the training set used to fit the model. The model names
# end with the fraction as well (without the dot)
atom.results
Out[5]:
| f1_train | f1_test | time_fit | f1_bootstrap | time_bootstrap | time | ||
|---|---|---|---|---|---|---|---|
| frac | model | ||||||
| 0.1 | LR01 | 0.5622 | 0.5852 | 0.720655 | 0.585044 | 0.728664 | 1.449319 |
| 0.2 | LR02 | 0.5830 | 0.5845 | 0.852776 | 0.585144 | 0.864794 | 1.717570 |
| 0.3 | LR03 | 0.5795 | 0.5856 | 1.085709 | 0.586101 | 1.119410 | 2.205119 |
| 0.4 | LR04 | 0.5847 | 0.5858 | 1.173066 | 0.586305 | 1.282166 | 2.455232 |
| 0.5 | LR05 | 0.5836 | 0.5862 | 1.264150 | 0.585003 | 1.460329 | 2.724479 |
| 0.6 | LR06 | 0.5832 | 0.5833 | 1.391943 | 0.584966 | 1.703550 | 3.095493 |
| 0.7 | LR07 | 0.5880 | 0.5856 | 1.585444 | 0.585199 | 1.835532 | 3.420976 |
| 0.8 | LR08 | 0.5914 | 0.5882 | 1.693054 | 0.585235 | 2.138652 | 3.831706 |
| 0.9 | LR09 | 0.5854 | 0.5828 | 1.753595 | 0.584420 | 2.353141 | 4.106736 |
| 1.0 | LR10 | 0.5862 | 0.5850 | 1.977799 | 0.584634 | 2.543574 | 4.521373 |
In [6]:
Copied!
# Every model can be accessed through its name
atom.lr05.plot_shap_waterfall(show=6)
# Every model can be accessed through its name
atom.lr05.plot_shap_waterfall(show=6)
In [7]:
Copied!
# Plot the train sizing's results
atom.plot_learning_curve()
# Plot the train sizing's results
atom.plot_learning_curve()